package com.glory.dgcpcpda.activity.technicalservice;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.base.Constant;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.RepairFromEntity;
import com.glory.dgcpcpda.entity.RepairSheetDataEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TechnicalReportActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etEqpWork)
    FreeUI_ClearEditText etEqpWork;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.tvEqpLocation)
    TextView tvEqpLocation;
    @BindView(R.id.rbPart)
    RadioButton rbPart;
    @BindView(R.id.rbMaterial)
    RadioButton rbMaterial;
    @BindView(R.id.rbTooling)
    RadioButton rbTooling;
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvPriority)
    TextView tvPriority;
    @BindView(R.id.ivPriorityArrow)
    ImageView ivPriorityArrow;
    @BindView(R.id.flPriorityList)
    FrameLayout flPriorityList;
    @BindView(R.id.etOwner)
    EditText etOwner;
    private String mRepairType = "";
    private List<String> mPriorityList;
    private FreeUI_GeneralPop mPriorityPop;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_technical_report;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("技术需求提报");
        mPriorityList = new ArrayList<>();
        mPriorityList.add("高");
        mPriorityList.add("中");
        mPriorityList.add("低");

        initEdit(etEqpWork, s -> LotUtils.searchEqpInfo(s, TechnicalReportActivity.this, getSupportFragmentManager(), new LotUtils.netCallback<EqpInfoEntity>() {
            @Override
            public void onSuccess(EqpInfoEntity response) {
                tvEqpLocation.setText(response.getEQUIPMENT().getLOCATION());
            }

            @Override
            public void onFail(Throwable t) {
                loadError(t,"EQPQUERY");
            }
        }));
    }

    @Override
    protected void initData() {

    }


    @OnClick({R.id.tvBaseSubmit, R.id.rbPart, R.id.rbMaterial, R.id.rbTooling,R.id.flPriorityList})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (mRepairType.equals("")) {
                    showToast("请选择需求类型", false);
                } else if (getText(etLotID).equals("")) {
                    showToast("请扫描批次号", false);
                } else if (getText(etEqpWork).equals("")) {
                    showToast("请扫描设备编号", false);
                } else if (tvPriority.getText().toString().equals("")){
                    showToast("请选择优先级",false);
                }else if (getText(etOwner).equals("")){
                    showToast("请输入操作员",false);
                }else {
                    showSubmitDialog(() -> submit());
                }
                break;
            case R.id.rbPart:
                mRepairType =  "changeProducct";
                break;
            case R.id.rbMaterial:
                mRepairType = "changeMaterial";
                break;
            case R.id.rbTooling:
                mRepairType = "changeTool";
                break;
            case R.id.flPriorityList:
                if (mPriorityPop != null) {
                    if (mPriorityPop.isShowing()) {
                        mPriorityPop.dismiss();
                    }
                } else {
                    mPriorityPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flPriorityList.getWidth(), mPriorityList, ivPriorityArrow, (codeS, i) -> {
                        tvPriority.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivPriorityArrow);
                mPriorityPop.showAsDropDown(flPriorityList, 0, 0);
                break;
        }
    }

    private void submit() {
        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showConfirmDialog(TechnicalReportActivity.this, "提交成功");
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(), "PMSREPAIR");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "START");
        map.addProperty("REPAIRTYPE", mRepairType);
        RepairFromEntity repairFromEntity = new RepairFromEntity(getText(etOwner), getText(etEqpWork));
        repairFromEntity.setCOMMENTS(getText(etComment));
        map.add("REPAIRFORM", new JsonParser().parse(new Gson().toJson(repairFromEntity)).getAsJsonObject());
        RepairSheetDataEntity repairSheetDataEntity = new RepairSheetDataEntity();
        repairSheetDataEntity.setEQUIPMENTID(getText(etEqpWork));
        repairSheetDataEntity.setLOTID(getText(etLotID));
        repairSheetDataEntity.setPRIORITY(tvPriority.getText().toString());
        //SHIJIAN
        repairSheetDataEntity.setTROUBLETIME(FreeApi_DateUtils.getNowStringDate("yyyy/MM/dd HH:mm:ss"));
        repairSheetDataEntity.setTROUBLEREPORTER(getText(etOwner));
        repairSheetDataEntity.setOWNER(getText(etOwner));
        map.add("REPAIRSHEETDATA", new JsonParser().parse(new Gson().toJson(repairSheetDataEntity)).getAsJsonObject());
        NetUtils.mRequestPostNet("CPC.PMSREPAIR", getText(etOwner).equals("")?Constant.CURRENT_USER:getText(etOwner),"PMSREPAIR", this, map, callBack);
    }


}
